package ai.bussness;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;


/**
 * Created by 杨济瑞 on 2016/8/9.
 */

public class AddInfo {
    Connection conn;
    PreparedStatement ps;

    public static void main(String[] args) {
        AddInfo addInfo = new AddInfo();
        System.out.println(addInfo.addMember("NA", "DD", "DA"));
        addInfo.addCard(" "," ");

    }

    /**
     * 通用的链接数据库方法
     *
     * @return 一个链接对象
     */
    public Connection getConnection() {


        String url = "jdbc:mysql://localhost:3306/yangjirui?"
                + "user=root&password=320925&useUnicode=true&characterEncoding=UTF8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("not found driver");
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url);
            if (conn != null) {
                System.out.println("connection successful~~~~");
            }
        } catch (SQLException e) {
            System.out.println("connection fail!!!!");
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 添加会员的信息
     *
     * @param name    姓名
     * @param address 地址
     * @param job     职业
     * @return 序号
     */
    public int addMember(String name, String address, String job) {

        int number = 0;
        String sql = "insert into member values (null,?,?,?)";

        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, address);
            ps.setString(3, job);
            number = ps.executeUpdate();
            System.out.println("正在写入人数据。。。");

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                ps.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return number;
    }


    /**
     * 保存年卡的数据
     * @param name  姓名
     * @param type  年卡类型
     */
    public void addCard(String name,String type){
        String sql = "insert into card values (null,?,?,?)";
        try {

            //设定卡日期
            SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            String startTime = sFormat.format(calendar.getTime());
            calendar.add(Calendar.YEAR, +1);
            String endTime = sFormat.format(calendar.getTime());
            java.sql.Date startDate=java.sql.Date.valueOf(startTime);
            java.sql.Date endDate=java.sql.Date.valueOf(endTime);

            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, -1);
            ps.setObject(2, startDate);
            ps.setObject(3, endDate);
            ps.executeUpdate();
            System.out.println("正在写入卡数据。。。");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存计数卡的信息
     * @param name 姓名
     * @param type 类型
     * @param count 计数次数
     */
    public void addCard(String name,String type,int count){
        String sql = "insert into card values (null,?,?,?)";
        try {

            SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            String startTime = sFormat.format(calendar.getTime());
            calendar.add(Calendar.YEAR, +1);
            String endTime = sFormat.format(calendar.getTime());
            java.sql.Date startDate=java.sql.Date.valueOf(startTime);
            java.sql.Date endDate=java.sql.Date.valueOf(endTime);

            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, count);
            ps.setObject(2, startDate);
            ps.setObject(3, endDate);
            ps.executeUpdate();
            System.out.println("正在写入卡数据。。。");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
